import Context from "./Context";
import DecorViewManager from "./DecorViewManager";
import PopupWindowManager from "./PopupWindowManager";
import WindowManager from "./WindowManager";
import {wxEventAdapter} from "./wx/WxEvent";
import Component from "./Component";
import Wx from "./wx/Wx";
import {logger} from "../app/third-scripts/game_sdk/Logger";

declare const require: any;
require("./utils/events/ccNodeEx");

const {ccclass, property} = cc._decorator;
/**
 * 程序入口类.用户初始化启动场景.
 */

@ccclass
export default class App extends Component {
    public get context(): Context {
        return this._context;
    }

    @property(WindowManager)
    protected content: WindowManager = null;

    @property(DecorViewManager)
    protected decorViewManager: DecorViewManager = null;

    @property(PopupWindowManager)
    protected popupWindowManager: PopupWindowManager = null;

    @property(cc.Sprite)
    protected background: cc.Sprite = null;

    private _context: Context = null;

    public onLoad() {
        this._context = new Context(this);
        wxEventAdapter.setup(this._context);
        Wx.onMemoryWarning(this.onMemoryWarning);
        this.beforeLoad();
    }

    public getContent(): WindowManager {
        return this.content;
    }

    public getPopupManager(): PopupWindowManager {
        return this.popupWindowManager;
    }

    public getBackground(): cc.Sprite {
        return this.background;
    }

    public getDecorManager(): DecorViewManager {
        return this.decorViewManager;
    }

    protected beforeLoad() {

    }

    protected onMemoryWarning(level: number) {
        cc.log("内存不足警告:", level);
        logger.sendEvent("内存不足", {lv: level});
    }

    protected setCollidEnable(enable: boolean, debugEnable: boolean = true) {

        const cM = cc.director.getCollisionManager();
        if (cM) {
            cM.enabled = enable;
        }
        const pM = cc.director.getPhysicsManager();
        if (pM) {
            pM.enabled = enable;
        }
        if (!pM || !cM) {
            console.warn("物理引擎模块未选择.请注意.")
        }
        if (CC_DEV && enable && debugEnable && cM && pM) {
            cM.enabledDebugDraw = true;
            cM.enabledDrawBoundingBox = true;
            // @ts-ignore
            pM.debugDrawFlags =
                // @ts-ignore
                cc.PhysicsManager.DrawBits.e_aabbBit |
                // @ts-ignore
                cc.PhysicsManager.DrawBits.e_pairBit |
                // @ts-ignore
                cc.PhysicsManager.DrawBits.e_centerOfMassBit |
                // @ts-ignore
                cc.PhysicsManager.DrawBits.e_jointBit |
                // @ts-ignore
                cc.PhysicsManager.DrawBits.e_shapeBit;
        }
    }
}
